package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.cons.TestCons;
import com.xrz.sxm.aj.entity.SerializableMap;
import com.xrz.sxm.aj.entity.Test;
import com.xrz.sxm.aj.entity.TestItem;
import com.xrz.sxm.aj.utils.BaseDialog;
import com.xrz.sxm.aj.utils.BaseDialog.onDialogBtnClickListener;

import java.util.List;

public class Act_Test extends BaseAct implements OnClickListener {

	@ViewInject(R.id.viewflipper)
	private ViewFlipper mViewFlipper;

	private List<Test> m_Tests;

	/** 当前是第几页 */
	private int m_Curr_Page = 0;

	/** 上一题按钮 */
	@ViewInject(R.id.btn_pre)
	private Button btn_pre;
	/** 下一题按钮 */
	@ViewInject(R.id.btn_next)
	private Button btn_next;

	@Override
	protected int setView() {
		return R.layout.layout_test;
	}

	private void showTBox() {
		BaseDialog.showDialog(ctx, "系统提示", "是否退出测试？", false, "取消", "退出", null,
				new onDialogBtnClickListener() {
					@Override
					public void onClick() {
						finish();
					}
				});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showTBox();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void init() {
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showTBox();
			}
		});
		ScrollView scr = new ScrollView(ctx);
//		LinearLayout lay = new LinearLayout(ctx);
//		lay.setOrientation(LinearLayout.VERTICAL);
//		View mview = LayoutInflater.from(ctx).inflate(
//				R.layout.shouye_flipper_test, null);
//		lay.addView(mview);
//		scr.addView(lay);
//		mViewFlipper.addView(scr);
		for (int i = 0; i < m_Tests.size(); i++) {
			ScrollView scrollView = new ScrollView(ctx);
			LinearLayout layout = new LinearLayout(ctx);
			layout.setOrientation(LinearLayout.VERTICAL);
			for (int j = 0; j < m_Tests.get(i).list.size(); j++) {
				TestItem testItem = m_Tests.get(i).list.get(j);
				View view = LayoutInflater.from(ctx).inflate(
						R.layout.item_flipper_test, null);
				RadioGroup radioGroup = (RadioGroup) view
						.findViewById(R.id.rg_test);
				radioGroup.setOnCheckedChangeListener(new RadioListener(
						testItem));
				TextView tv = (TextView) view.findViewById(R.id.tv_wenti);
				tv.setText((j + 1) + "、" + testItem.wenti);
				layout.addView(view);
			}
			scrollView.addView(layout);
			mViewFlipper.addView(scrollView);
		}
		btn_pre.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		if (m_Curr_Page == m_Tests.size()) {
			btn_pre.setVisibility(View.VISIBLE);
			btn_next.setVisibility(View.INVISIBLE);
		} else {
			btn_pre.setVisibility(View.INVISIBLE);
			btn_next.setVisibility(View.VISIBLE);
		}
		refreshButton();
	}

	@Override
	protected String setTopTitle() {
		m_Tests = TestCons.getTests();
		// return "测试题 1/" + m_Tests.size();
		return "测试体质";
	}

	private void refreshButton() {
		if (m_Curr_Page == m_Tests.size() - 1) {
			btn_pre.setVisibility(View.VISIBLE);
			btn_next.setVisibility(View.VISIBLE);
			btn_next.setText("查看测试结果");
		} else {
			btn_pre.setVisibility(View.VISIBLE);
			btn_next.setVisibility(View.VISIBLE);
			btn_next.setText("下一页");
		}
	}

	/**
	 * 检测是否选择了
	 */
	private boolean checkIsSelect() {
			for (TestItem testItem : m_Tests.get(m_Curr_Page ).list) {
				if (testItem.selectIndex == -1) {
					return false;
				}
			}
			return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		for (Test test : m_Tests) {
			for (TestItem item : test.list) {
				item.selectIndex = -1;
			}
		}
	}

	// 切换下一张图片
	private void flipperShowNext() {
		if (m_Curr_Page == m_Tests.size()) {
			return;
		}
		m_Curr_Page++;
		mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_in));
		mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_out));
		mViewFlipper.showNext();
		reImageView(m_Curr_Page + 1);
	}

	// 切换上一张图片
	private void flipperShowPre() {
		if (m_Curr_Page == 0) {
			return;
		}
		m_Curr_Page--;

		mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_right_in));
		mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_right_out));
		mViewFlipper.showPrevious();
		reImageView(m_Curr_Page + 1);
	}

	// 更新几点小店
	private void reImageView(int nums) {
			titleTv.setText("测试题 " + (nums) + "/" + m_Tests.size());
	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
			case R.id.btn_pre:
				flipperShowPre();
				break;
			case R.id.btn_next:
				if (checkIsSelect() == false) {
					toastLong("请选择一项");
					return;
				}
				checkIsLast();
				break;
		}
		refreshButton();
	}

	private void checkIsLast() {
		if (m_Curr_Page == m_Tests.size() - 1) {
			jisuan();
		} else {
			flipperShowNext();
		}
	}

	/**
	 * 计算结果
	 */
	private void jisuan() {
		Intent intent = new Intent(this, Act_chat.class);
		SerializableMap tmpmap = new SerializableMap();
		tmpmap.setMap(m_Tests);
		intent.putExtra("data", tmpmap);
		startActivity(intent);
		finish();
	}

	private class RadioListener implements OnCheckedChangeListener {

		private TestItem test;

		public RadioListener(TestItem test) {
			this.test = test;
		}

		@Override
		public void onCheckedChanged(RadioGroup arg0, int arg1) {
			switch (arg1) {
				case R.id.rb_1:
					test.selectIndex = 0;
					break;
				case R.id.rb_2:
					test.selectIndex = 1;
					break;
				case R.id.rb_3:
					test.selectIndex = 2;
					break;
				case R.id.rb_4:
					test.selectIndex = 3;
					break;
				case R.id.rb_5:
					test.selectIndex = 4;
					break;
			}
			// flipperShowNext();
			refreshButton();
		}
	}

	@Override
	protected boolean isBackBtnClickFinish() {
		return false;
	}
}
